﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class ClassAttributeDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="ClassAttributeID">标识列ID</param>
		public bool Exists(int ClassAttributeID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [ClassAttribute] where ClassAttributeID=@classattributeid ");
			htSyn.Add("@classattributeid", ClassAttributeID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<ClassAttribute> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [ClassAttributeID],[pClassID],[ClassRelationMap],[cDescription],[ClassDescription],[cTitle],[cKey] FROM [ClassAttribute]");
			 return getClassAttributeBySql(strSql.ToString());
		}
		#endregion

        #region 根据类别id获得实体---ztf
        /// <summary>
        /// 根据类别id获得实体---ztf
        /// </summary>
        /// <param name="pClassID"></param>
        /// <returns></returns>
        public ClassAttribute GetModelByClassID(int pClassID) 
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select [ClassAttributeID],[pClassID],[ClassRelationMap],[cDescription],[ClassDescription],[cTitle],[cKey] FROM [ClassAttribute]");
            strSql.Append(" where pClassID=@pclassid ");
            htSyn.Add("@pclassid", pClassID );
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            ClassAttribute classattributeModel = new ClassAttribute();
            if (dt.Rows.Count > 0)
            {
                classattributeModel.pClassID = Convert.ToInt32(dt.Rows[0]["pClassID"]);
                classattributeModel.classRelationMap = dt.Rows[0]["ClassRelationMap"].ToString();
                classattributeModel.cDescription = dt.Rows[0]["cDescription"].ToString();
                classattributeModel.classDescription = dt.Rows[0]["ClassDescription"].ToString();
                classattributeModel.cTitle = dt.Rows[0]["cTitle"].ToString();
                classattributeModel.cKey = dt.Rows[0]["cKey"].ToString();
                classattributeModel.classAttributeID = Convert.ToInt32(dt.Rows[0]["ClassAttributeID"]);
            }
            return classattributeModel;
        }
        #endregion

        #region 根据标识列查询实体
        /// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="ClassAttributeID">标识列ID</param>
		/// <returns></returns>
		public ClassAttribute GetModel(int classattributeid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [ClassAttributeID],[pClassID],[ClassRelationMap],[cDescription],[ClassDescription],[cTitle],[cKey] FROM [ClassAttribute]");
			strSql.Append(" where ClassAttributeID=@classattributeid ");
			htSyn.Add("@classattributeid", classattributeid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			ClassAttribute classattributeModel = new ClassAttribute();
			if (dt.Rows.Count > 0)
			{
				classattributeModel.pClassID=Convert.ToInt32(dt.Rows[0]["pClassID"]);
				classattributeModel.classRelationMap=dt.Rows[0]["ClassRelationMap"].ToString();
				classattributeModel.cDescription=dt.Rows[0]["cDescription"].ToString();
				classattributeModel.classDescription=dt.Rows[0]["ClassDescription"].ToString();
				classattributeModel.cTitle=dt.Rows[0]["cTitle"].ToString();
				classattributeModel.cKey=dt.Rows[0]["cKey"].ToString();
			}
			return classattributeModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="ClassAttribute">实体</param>
		/// <returns></returns>
		public bool Update(ClassAttribute classattributeModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [ClassAttribute] set ");
			strSql.Append("[pClassID]=@pclassid,");
			strSql.Append("[ClassRelationMap]=@classrelationmap,");
			strSql.Append("[cDescription]=@cdescription,");
			strSql.Append("[ClassDescription]=@classdescription,");
			strSql.Append("[cTitle]=@ctitle,");
			strSql.Append("[cKey]=@ckey");
			strSql.Append(" where ClassAttributeID=@classattributeid ");
			htSyn.Add("@classattributeid", classattributeModel.classAttributeID);
			htSyn.Add("@pclassid", classattributeModel.pClassID);
			htSyn.Add("@classrelationmap", classattributeModel.classRelationMap);
			htSyn.Add("@cdescription", classattributeModel.cDescription);
			htSyn.Add("@classdescription", classattributeModel.classDescription);
			htSyn.Add("@ctitle", classattributeModel.cTitle);
			htSyn.Add("@ckey", classattributeModel.cKey);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="ClassAttributeID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int ClassAttributeID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [ClassAttribute] where ClassAttributeID=@classattributeid");
			htSyn.Add("classattributeid", ClassAttributeID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="ClassAttribute">实体</param>
		/// <returns></returns>
		public bool Insert(ClassAttribute classattributeModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [ClassAttribute]([pClassID],[ClassRelationMap],[cDescription],[ClassDescription],[cTitle],[cKey]) values (@pclassid,@classrelationmap,@cdescription,@classdescription,@ctitle,@ckey)");
			htSyn.Add("@pclassid", classattributeModel.pClassID);
			htSyn.Add("@classrelationmap", classattributeModel.classRelationMap);
			htSyn.Add("@cdescription", classattributeModel.cDescription);
			htSyn.Add("@classdescription", classattributeModel.classDescription);
			htSyn.Add("@ctitle", classattributeModel.cTitle);
			htSyn.Add("@ckey", classattributeModel.cKey);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<ClassAttribute> getClassAttributeBySql(string strSql)
		{
			IList<ClassAttribute> list = new List<ClassAttribute>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					ClassAttribute classattributeModel = new ClassAttribute();
					classattributeModel.classAttributeID= Convert.ToInt32(dr["ClassAttributeID"]);
					classattributeModel.pClassID= Convert.ToInt32(dr["pClassID"]);
					classattributeModel.classRelationMap=dr["ClassRelationMap"].ToString();
					classattributeModel.cDescription=dr["cDescription"].ToString();
					classattributeModel.classDescription=dr["ClassDescription"].ToString();
					classattributeModel.cTitle=dr["cTitle"].ToString();
					classattributeModel.cKey=dr["cKey"].ToString();
					list.Add(classattributeModel);
				}
			}
			return list;
		}
		#endregion
	}
}
